package com.ngc.mall.manager.redis;

import com.ngc.mall.bo.OrderMessageBO;
import com.ngc.mall.service.ICouponService;
import com.ngc.mall.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 *
 * 未支付订单过期通知 监听 7
 * @author: chenxiaolin
 * @date 2023/10/11 17:46
**/
@Component
public class TopicMessageListener implements MessageListener {
    @Autowired
    private ICouponService iCouponService;

    @Autowired
    private IOrderService iOrderService;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        byte[] body = message.getBody();
        byte[] channel = message.getChannel();

        String expiredKey = new String(body);
        String topic = new String(channel);
        OrderMessageBO messageBO = new OrderMessageBO(expiredKey);
        iCouponService.returnBack(messageBO);
        iOrderService.cancel(messageBO.getOrderId());
    }
}
